package com.sw.core.doc.doc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.jfinal.util.PathUtil;
import com.sw.core.doc.DocUtilty;
import com.sw.core.doc.txt.TxtBaseOpr;
import com.sw.core.exception.DocOprException;
import com.sw.core.expopr.WKAppException;
import com.sw.core.memcache.MemCache;
import com.tapestry.app.core.data.IList;
import com.tapestry.app.core.data.IMap;
import com.tapestry.app.core.data.dataimp.DataList;
import com.tapestry.app.core.data.dataimp.DataMap;

/**
 * @author:YF
 * @Dat:   2014年1月23日   上午10:14:14 
 * @version: v1.0
 * @Description : poi操作office文档
 */
public final class DocBaseOpr {
	
	/**
	 * @Title : coventFileStr
	 * @Description:转换文件路径
	 * @Param : @param path
	 * @Param : @param fileName
	 * @Param : @return
	 * @return: String
	 * @throws IOException 
	 */
	public static String coventFileStr(String path,String fileName,String filetype) throws IOException{
		String file = "";
		String refileName = "";
		DocUtilty du = new DocUtilty();
		if(path.length() <= 0 || path == null || "".equals(path)){
			path = getDefaultPath();
		}
		if(path.indexOf("\\") != -1){
			path.replace("\\", "/");
		}
		if(!path.endsWith("/")){
			path += "/";
		}
		if(fileName.length() <= 0 || fileName == null || "".equals(fileName)){ //未输入文件名
			fileName = du.getRandomStr(8);
		}
		file = path + fileName;
		if(!file.endsWith("."+filetype)){
			file += "."+filetype;
		}
		File fileExit = new File(file);
		while(fileExit.exists()){
			fileName = du.getRandomStr(8);
			file = path + fileName;
			if(!file.endsWith("."+filetype)){
				file += "."+filetype;
			}
			fileExit = new File(file);
		}
		return file;
	}
	
	public static IList ReadXls(){
		return null;
		
	}
	
	
	/**
	 * @Title : getDefaultPath
	 * @Description:获取默认文件路径，如果默认目录不存在，则创建目录
	 * @Param : @return
	 * @Param : @throws IOException
	 * @return: String
	 */
	private static String getDefaultPath() throws IOException{
		TxtBaseOpr tbo = new TxtBaseOpr();
		String file = "classes/config";
		String defaultpath = "";
		String configfile = "";
		String fullFile;
		if (file.startsWith(File.separator)){
			fullFile = PathUtil.getWebRootPath() + File.separator + "WEB-INF" + file;
		}else{
			fullFile = PathUtil.getWebRootPath() + File.separator + "WEB-INF" + File.separator + file;
		}
		File configfiles = new File(fullFile);
		if(!configfiles.exists()){
			WKAppException.apperr(DocOprException.DOC_EXCEPTION_2);
		}
		IMap data = tbo.getProperties(fullFile);
		defaultpath = data.getString("defaultpath");
		if(defaultpath == null || defaultpath.length() <= 0){
			WKAppException.apperr(DocOprException.DOC_EXCEPTION_3);
		}
		configfile = data.getString("defaultpath");
		File defaultfilepath = new File(configfile);
		if(!defaultfilepath.exists()){
			defaultfilepath.mkdirs();
		}
		return configfile;
	}
	
	
	/**
	 * @Title : main
	 * @Description:test
	 * @Param : @param args
	 * @Param : @throws Exception
	 * @return: void
	 */
	public static void main(String[] args) throws Exception{
		
		//******************************************************造数据***************************
		String path = "D:/TEMP/TEMP";
		String filename = "test";
		IList list = new DataList();
		IMap data = new DataMap();
		for(int i = 0 ; i < 5 ; i++){
			data.put("key"+i, "value"+i);
		}
		list.add(data);
		list.add(data);
		list.add(data);
		//******************************************************造数据***************************
		
		
//		createExcel(path,filename);   //生成一个空的xls文件
//		createDataExcel(path,filename,list);  //生成一个数据为list的xls文件
		
		DocUtilty du = new DocUtilty();
		System.out.println(du.getRandomStr(8));
	}

}
